package com.atguigu.lease.common.result;

import lombok.Data;

/**
 * 全局统一返回结果类
 */
@Data // jackson 序列化需要用到getter
public class Result<T> {

    //返回码
    private Integer code;

    //返回消息
    private String message;

    // 是否成功
    private Boolean flag;

    //返回数据
    private T data;



    public Result() {
    }

    private static <T> Result<T> build(T data) {
        Result<T> result = new Result<>();
        if (data != null)
            result.setData(data);
        return result;
    }

    public static <T> Result<T> build(T data, ResultCodeEnum resultCodeEnum, boolean flag) {
        Result<T> result = new Result<>();
        result.setData(data);
        result.setCode(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        result.setFlag(flag);
        return result;
    }

    public static <T> Result<T> build(boolean flag) {
        //根据布尔类型的值创建默认的成功或者失败对象返回
        return flag ? Result.ok() : Result.fail();
    }
    public static <T> Result<T> build(Integer code , String message , boolean flag , T data){
        Result<T> result = new Result<>();
        result.code = code;
        result.message = message;
        result.flag = flag;
        result.data = data;
        return result;
    }



    public static <T> Result<T> ok(T data) {
        return build(data, ResultCodeEnum.SUCCESS, true);
    }

    public static <T> Result<T> ok() {
        return Result.ok(null);
    }

    public static <T> Result<T> fail(ResultCodeEnum codeEnum) {
        return build(null, codeEnum, false);
    }

    public static <T> Result<T> fail() {
        return build(null, ResultCodeEnum.FAIL, false);
    }
}
